package com.sitanyun.merchant.guide.callback;

import android.util.Log;

import com.google.gson.Gson;
import com.sitanyun.merchant.guide.bean.SwiShopsBean;
import com.sitanyun.merchant.guide.url.Urls;
import com.sitanyun.merchant.guide.weiht.Aes;
import com.sitanyun.merchant.guide.weiht.SharedPreferenceUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.HashMap;

import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.Response;

/**
 * Created by jzl
 * on 2022/2/21.
 */
public abstract class StringCallbacks extends Callback<String>
{
    @Override
    public String parseNetworkResponse(Response response, int id) throws Exception {
        if(response.code()==0){
          //  return response.body().string();
        }else{
         //   throw new Exception("code is:"+response.code()+"\n"+response.body().string());
           onErrors(response.code());
        }
       return response.body().string();
    }

    private void onErrors(int code) {
        Log.e("CODEs",code+"");
       if (code==401){
           Aes mAes = new Aes();
           try {
               String secret=  SharedPreferenceUtil.getStringData("cid")
                       +","+SharedPreferenceUtil.getStringData("nodeid")+","
                       +SharedPreferenceUtil.getStringData("staffid")+","
                       +SharedPreferenceUtil.getStringData("userid");
               byte[]  secrets = secret.getBytes("UTF8");
               String   secretaec = mAes.encrypt(secrets);//加密

               HashMap<String, String> hashMap = new HashMap<String, String>();
               hashMap.put("staffId",SharedPreferenceUtil.getStringData("staffid"));
               hashMap.put("userId", SharedPreferenceUtil.getStringData("userid"));
               hashMap.put("clientId",  SharedPreferenceUtil.getStringData("cid"));//
               hashMap.put("nodeId", SharedPreferenceUtil.getStringData("nodeid") );//

               hashMap.put("secret",  secretaec);//

               OkHttpUtils.postString()
                       .mediaType(MediaType.parse("application/json; charset=utf-8"))
                       .addHeader("Content-Type", "application/json")
                       //   .addHeader("Authorization", "Bearer " + SharedPreferenceUtil.getStringData("token"))
                       .url(Urls.manual)
                       .content(new Gson().toJson(hashMap))
                       .build().execute(new StringCallback() {
                   @Override
                   public void onError(Call call, Exception e, int id) {
                   }
                   @Override
                   public void onResponse(String response, int id) {
                       SwiShopsBean loginBean= new Gson().fromJson(response, SwiShopsBean.class);
                       if (loginBean.getCode() == 0) {
                           SharedPreferenceUtil.SaveData("userid", loginBean.getUser_id());
                           SharedPreferenceUtil.SaveData("nodename", loginBean.getNode_name());
                           SharedPreferenceUtil.SaveData("tonodename", loginBean.getTop_node_name()+"");
                           SharedPreferenceUtil.SaveData("token", loginBean.getAccess_token());
                           SharedPreferenceUtil.SaveData("staffid", loginBean.getStaff_id());
                           SharedPreferenceUtil.SaveData("clientid", loginBean.getS_client_id());
                           SharedPreferenceUtil.SaveData("nodeid",loginBean.getNode_id());
                           SharedPreferenceUtil.SaveData("tonodeid", loginBean.getTop_node_id());
                           SharedPreferenceUtil.SaveData("sxtoken", loginBean.getRefresh_token());
                           SharedPreferenceUtil.SaveData("cid", loginBean.getS_client_id());
                           SharedPreferenceUtil.SaveData("staffname", loginBean.getStaff_name());


                       } else {

                       }
                   }
               });
           } catch (Exception e) {
               e.printStackTrace();
           }

       }
    }

}
